<?php

namespace App\Services;

use App\Models\AutoReply;
use App\Models\Material;
use EasyWeChat\Kernel\Messages\Image;
use EasyWeChat\Kernel\Messages\Media;
use EasyWeChat\Kernel\Messages\Text;
use Overtrue\LaravelWeChat\Facade as EasyWechat;

class WechatCustomService
{
    public static function send(AutoReply $reply, $toOpenid)
    {
        $app = EasyWechat::officialAccount();
        $message = self::formatMessage($reply);
        if ($message) {
            $app->customer_service->message($message)->to($toOpenid)->send();
        }
    }

    protected static function formatMessage(AutoReply $reply)
    {
        switch ($reply->type) {
            case Material::TYPE_TEXT:
                $message = new Text($reply->content);
                break;
            case Material::TYPE_IMAGE:
                $message = new Image($reply->material->media_id);
                break;
            case Material::TYPE_NEWS:
                $message = new Media($reply->material->media_id, 'mpnews');
                break;
            default:
                return null;
        }
        return $message;
    }
}